﻿using MyFile.Config;
using MyFile.Service;
using HslCommunication;
using HslCommunication.Profinet.Siemens;

namespace MyFile.Init
{
    public class SiemensInit
    {
        private static readonly Lazy<SiemensInit> lazy = new Lazy<SiemensInit>(() => new SiemensInit());
        public static SiemensInit Instance { get { return lazy.Value; } }
        public SiemensConfig config = new SiemensConfig();
        List<DBData> dBs = new List<DBData>();
        public SiemensS7Net service;
        public SiemensInit() { }
        

        public void Init()
        {
            config.IP = "127.0.0.1";
            dBs.Add(new DBData() { DBAddress = "M100", Status = false });
            dBs.Add(new DBData() { DBAddress = "M101", Status = false });
            dBs.Add(new DBData() { DBAddress = "M102", Status = false });
            dBs.Add(new DBData() { DBAddress = "M103", Status = false });
            dBs.Add(new DBData() { DBAddress = "M104", Status = false });
            dBs.Add(new DBData() { DBAddress = "M105", Status = false });
            dBs.Add(new DBData() { DBAddress = "M106", Status = false });
            dBs.Add(new DBData() { DBAddress = "M107", Status = false });
            dBs.Add(new DBData() { DBAddress = "M108", Status = false });
            dBs.Add(new DBData() { DBAddress = "M109", Status = false });
            dBs.Add(new DBData() { DBAddress = "M110", Status = false });
            config.S7DB = dBs;
            config.ConnectStatus = ENUM_CONNECT_STATUS.连接失败;
        }
        public void Start()
        {
            service = new SiemensS7Net(SiemensPLCS.S200Smart,config.IP);
            OperateResult serviceConnect = service.ConnectServer();
            if(serviceConnect.IsSuccess)
            {
                config.ConnectStatus = ENUM_CONNECT_STATUS.连接成功;
                Console.WriteLine(config.ConnectStatus);
            }
            else
            {
                config.ConnectStatus = ENUM_CONNECT_STATUS.连接失败;
                Console.WriteLine(config.ConnectStatus);
            }
        }
        public void Close()
        {
            service.Dispose();
        }
    }
}
